package palmtop.com.palmtop.activity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.TextView;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

import org.xutils.common.Callback;
import org.xutils.http.RequestParams;

import java.lang.reflect.Type;
import java.util.ArrayList;

import butterknife.BindView;
import butterknife.ButterKnife;
import palmtop.com.palmtop.R;
import palmtop.com.palmtop.adapter.PopulationAdapter;
import palmtop.com.palmtop.adapter.ReportpopulationAdapter;
import palmtop.com.palmtop.model.Reportpopulation;
import palmtop.com.palmtop.util.Config;
import palmtop.com.palmtop.util.NetUtil;

public class PopulationActivity extends BaseActivity {

    @BindView(R.id.mtitle)
    TextView mtitle;
    @BindView(R.id.populationdetailList)
    ListView populationdetailList;

    private ArrayList<Reportpopulation> arrlist;
    private PopulationAdapter adapter;
    Reportpopulation reportpopulation;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_populationdetail, Config.toptitle, 1);
        MyApplication app = (MyApplication) getApplication();
        app.addActivityList(PopulationActivity.this);
        ButterKnife.bind(this);
        initView();
    }

    private void initView() {
        mtitle.setText("基础信息上报");
        arrlist = new ArrayList<Reportpopulation>();
        reportpopulation = (Reportpopulation)getIntent().getSerializableExtra("reportpopulation");
        if(reportpopulation!=null){
            RequestParams params = new RequestParams(Config.SERVER_URL + "reportpopulation/getByUnitid");
            params.addBodyParameter("unitid",String.valueOf(reportpopulation.getUnitid()));
            NetUtil.postData(params, new Callback.CommonCallback<String>() {
                @Override
                public void onSuccess(String result) {
                    Gson gson = new Gson();
                    Type listType = new TypeToken<ArrayList<Reportpopulation>>() {
                    }.getType();
                    final ArrayList<Reportpopulation> list = gson.fromJson(result,listType);
                    adapter = new PopulationAdapter(list,PopulationActivity.this);
                    populationdetailList.setAdapter(adapter);

//                    populationdetailList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
//                        @Override
//                        public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
//                            Intent intent = new Intent();
//                            intent.putExtra("reportpopulation",list.get(i));
//                            intent.setClass(PopulationActivity.this,PopulationdetailActivity.class);
//                            startActivity(intent);
//                        }
//                    });
                }

                @Override
                public void onError(Throwable ex, boolean isOnCallback) {}
                @Override
                public void onCancelled(CancelledException cex) {}
                @Override
                public void onFinished() {}
            });
        }



    }
}
